package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.ImageAdapter;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BrokerDailyBean;
import com.csweimei.rentmanagementtreasure.pop.ImageShowPop;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.dayPaper.DayPaperDetailActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class BDailyItemVM extends MultiItemViewModel {

    public BDailyItemVM(@NonNull BaseViewModel viewModel) {
        super(viewModel);
    }

    public ObservableField<BrokerDailyBean.DataDTO> entity = new ObservableField<>();
    public ObservableField<ImageAdapter> adapter = new ObservableField<>(new ImageAdapter());


    public void setData(BrokerDailyBean.DataDTO bean) {
        entity.set(bean);

        List<String> list = new ArrayList<>();
        if (bean.getImgsrc().contains(",")){
            list.addAll(Arrays.asList(bean.getImgsrc().split(",")));
        }else {
            list.add(bean.getImgsrc());
        }
        adapter.get().setList(list);
        adapter.get().addChildClickViewIds(R.id.img);
        adapter.get().setOnItemChildClickListener((adapter1, view, position) -> {
            showImgPop( adapter.get().getData().get(position));
        });

    }

    private void showImgPop(String s) {
        ImageShowPop imageShowPop = new ImageShowPop(viewModel.mActivity,s);
        PopUtils.showPop(viewModel.mActivity,imageShowPop,false);
    }

    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString(AppConfig.BUNDLE_KEY,entity.get().getId()+"");
            viewModel.startActivity(DayPaperDetailActivity.class,bundle);
        }
    });
}
